// pages/pointDetail/pointDetail.js
const app = getApp();
var utils = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product: {},
    chose: true, //点击兑换切换
    showmask: false, //点击出现弹窗
    jian: false,
    jia: true,
    num: 1, //需要兑换产品个数
    allnum:1,//总共能兑换多少个
    allIntegral:200,//总积分
    integral:199,//产品积分
    exchange:true,//点击兑换
    changeFont:'点击兑换',
    point:0,//消耗积分
    canclick:true
  },
  //点击出弹窗
  exchange() {
    this.setData({
      showmask: true
    })
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear',
      delay: 0
    });
    animation.opacity(0).bottom('-610rpx').step();
    this.setData({
      ani: animation.export()
    })
    setTimeout( ()=> {
      // 执行第二组动画
      animation.opacity(1).bottom(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        ani: animation
      })
    }, 200)
  },
  //关闭弹窗
  closeMask(){
    this.setData({
      showmask: false
    })
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear',
      delay: 0
    });
    animation.opacity(0).bottom('-610rpx').step();
    this.setData({
      ani: animation.export()
    })
  },
  closeMaskRn(){
    this.closeMask()
    this.setData({
      chose:true
    })
  },
  //减
  reduce(){
    let i = this.data.num;
    i--;
    if (i <=1) {
      i = 1;
      this.setData({
        jian: false
      })
    }
    this.setData({
      num: i,
      point:i*this.data.integral
    })
    if (this.data.head.over < this.data.num * this.data.product.integral) {//超出能兑换的个数
      this.setData({
        changeFont: '积分不足',
        canclick: false
      })
    } else {
      this.setData({
        changeFont: '点击兑换',
        canclick: true
      })
    }
  },
  //加
  add(){
    let i = this.data.num;
    i++;
    if(i>=99){
      i = 99;
      this.setData({
        jia: false
      })
    }
    this.setData({
      num:i,
      jian: true,
      point: i * this.data.integral
    })
    if (this.data.head.over < this.data.num * this.data.product.integral){//超出能兑换的个数
      this.setData({
        changeFont:'积分不足',
        canclick:false
      })
    }else{
      this.setData({
        changeFont: '点击兑换',
        canclick: true
      })
    }
  },
  //点击兑换
  exchangeFn(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    if (this.data.canclick){ //能兑换
      var sucFunc = function (that, res) {
        wx.hideLoading();
        if(res.data == 402){
          wx.showModal({
            title: '提示',
            content: '库存不足，请看看其他商品吧！',
            success(res) {
              return false
            }
          })
        }else if(res.data == 200){
          that.setData({
            chose: false
          })
        }else{
          wx.showModal({
            title: '提示',
            content: '网络错误，请稍后重试！',
            success(res) {
              return false
            }
          })
        }
        
      }
      var data = { openid: app.d.openid, id: that.data.product.id, status: 1, price: that.data.product.integral * that.data.num,num:that.data.num}
      utils.wxRequest(that, app.d.path, "Integral/addorder", data, sucFunc);
    } else {//不能兑换
      return false
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    var sucFunc = function (that, res) {

      that.setData({
        point: that.data.integral,
        imgPath: app.d.imgPath
      })
      if (res.data.people.over < res.data.res.integral) { //没有兑换的个数
        that.setData({
          exchange: false,
          changeFont: '积分不足',
          canclick: false
        })
      } else {//有兑换次数
        that.setData({
          exchange: true,
          changeFont: '点击兑换',
          canclick: true
        })
      }
      that.setData({
        product: res.data.res,
        head: res.data.people,
        status: res.data.status
      })
      wx.hideLoading();
      WxParse.wxParse('article', 'html', res.data.res.content, that, 5)
    }
    var data = { openid: app.d.openid, id: options.id }
    utils.wxRequest(that, app.d.path, "Integral/detail", data, sucFunc);


  },
  linqu:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    
    var sucFunc = function (that, res) {
      wx.hideLoading();
      if (res.data == 402) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请看看其他商品吧！',
          success(res) {
            return false
          }
        })
      } else if (res.data == 200) {
        wx.showModal({
          title: '提示',
          content: '领取成功',
          success(res) {
            wx.redirectTo({
              url: "/pages/coupon/coupon"
            })
          }
        })
        
      } else {
        wx.showModal({
          title: '提示',
          content: '网络错误，请稍后重试！',
          success(res) {
            return false
          }
        })
      }

    }
    var data = { openid: app.d.openid, id: that.data.product.id, status: 3, price: that.data.product.integral * that.data.num, num: that.data.num }
    utils.wxRequest(that, app.d.path, "Integral/addorder", data, sucFunc);
  },
  btn1:function(){
    wx.redirectTo({
      url: "/pages/coupon/coupon"
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})